package com.zzptc.zzsgz.bdsjws.activities;


import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.fragments.SelectContactFragment;
import com.zzptc.zzsgz.bdsjws.fragments.ShowAllContactFragment;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AddContactsActivity extends BaseActivity {
    List<Contact> list;
    List<Contact> selectlist = new ArrayList<Contact>();
    ShowAllContactFragment showAllContactFragment;
    SelectContactFragment selectContactFragment;
    @BindView(R.id.et_contacts)
    EditText etContacts;

    boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int getContentXML() {
        return R.layout.activity_add_contacts;
    }

    @Override
    public void initData() {
        list = MyApplication.getList();
        if (showAllContactFragment == null) {
            showAllContactFragment = new ShowAllContactFragment();
        }
        getFragmentManager().beginTransaction().replace(R.id.fl_show_contact, showAllContactFragment).addToBackStack("contact").commit();
    }

    @Override
    public void initView() {
        etContacts.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
                if (flag){
                    if (selectContactFragment == null){
                        selectContactFragment = new SelectContactFragment();
                    }
                    getFragmentManager().beginTransaction().hide(showAllContactFragment).add(R.id.fl_show_contact , selectContactFragment).addToBackStack("contact").commit();
                    flag = false;
                }

            }

            @Override
            public void onTextChanged(final CharSequence charSequence, int start, int before, int count) {
                final CharSequence c = charSequence.toString().replace(" ","").replace("-","");
                selectlist.clear();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for(int i = 0; i < list.size(); i++){
                            Contact contact = list.get(i);
                            if (contact.getName().contains(c)||contact.getPhonenum().toString().replace(" ","").replace("-","").contains(charSequence)){
                                selectlist.add(contact);
                            }
                        }


                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                selectContactFragment.getSelectContactList(selectlist);
                            }
                        });

                    }
                }).start();


            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (etContacts.getText().length() == 0){
                    if (getFragmentManager().getBackStackEntryCount() > 1){
                        getFragmentManager().popBackStack();
                    }
                    flag = true;
                }
            }
        });
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (getFragmentManager().getBackStackEntryCount() > 1) {
            getFragmentManager().popBackStack();

        } else {
            finish();
        }
    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        startActivity(new Intent(this, OneKeySOSActivity.class));
    }
}
